'use strict';
var dayjs = require('dayjs')
var duration = require('dayjs/plugin/duration')
dayjs.extend(duration)
module.exports = {
    /**
     * XXXnameXXX
     * @url client/booths/kh/getBoothsInfo 前端调用的url参数地址
     * data 请求参数
     * @param {String} params1  参数1
     */
    main: async (event) => {
        let {
            data = {}, userInfo, util, filterResponse, originalParam
        } = event;
        let {
            customUtil,
            uniID,
            config,
            pubFun,
            vk,
            db,
            _
        } = util;
        let {
            uid
        } = data;
        let res = {
            code: 0,
            msg: ""
        };
        let {
            order_id,
            order_no,
            realname,
            idcard,
            booth_name,
            booth_no,
            mobile,
            sign_url,
            contract,
        } = data;
        const transaction = await vk.baseDao.startTransaction();
        try {
            let dbName = "bidding-contract";
            let con_no=  vk.pubfn.createOrderNo("HTNO_");
            await vk.baseDao.add({
                db: transaction,
                dbName,
                dataJson: {
                    con_no: con_no,
                    user_id: uid,
                    order_id,
                    order_no,
                    realname,
                    idcard,
                    booth_name,
                    booth_no,
                    mobile,
                    sign_url,
                    contract,
                }
            });
            let num = await vk.baseDao.updateById({
                db: transaction,
                dbName: "bidding-order", // 表名
                id: order_id,
                dataJson: { 
                    status: 1,//签合同成功
                    contract_no:con_no
                }
            });
            await transaction.commit();
            res.code = num > 0?0:-1;
        } catch (error) {
            // 事务回滚
            await transaction.rollback();
            console.error(`transaction error`, err)
            return {
                code: -1,
                msg: "数据库写入异常,事务已回滚",
                err: {
                    message: err.message,
                    stack: err.stack
                }
            }
        }

        // 业务逻辑结束-----------------------------------------------------------
        return res;
    }
}